const sqlite3 = require('sqlite3').verbose();
const path = require('path');

const dbPath = path.join(__dirname, 'data', 'pph.sqlite');
const db = new sqlite3.Database(dbPath);

console.log('🔍 检查PPH记录的目标值字段...');

// 查询最新记录的目标值相关字段
db.all(`
SELECT
    id,
    record_date,
    line_name,
    material_code,
    target_pph,
    estimate_pph,
    calculated_pph,
    efficiency_rate,
    created_at
FROM pph_records
ORDER BY id DESC 
LIMIT 3
`, (err, rows) => {
    if (err) {
        console.error('❌ 查询失败:', err);
    } else {
        console.log(`\n📊 最新3条记录的目标值字段:`);
        rows.forEach((row, index) => {
            console.log(`\n📝 记录 ${index + 1} (ID: ${row.id}):`);
            console.log(`  日期: ${row.record_date}`);
            console.log(`  生产线: ${row.line_name}`);
            console.log(`  物料编码: ${row.material_code}`);
            console.log(`  🎯 目标PPH: ${row.target_pph}`);
            console.log(`  📈 预估PPH: ${row.estimate_pph}`);
            console.log(`  ⚡ 计算PPH: ${row.calculated_pph}`);
            console.log(`  📊 达成率: ${row.efficiency_rate}%`);
            console.log(`  创建时间: ${row.created_at}`);
        });
    }
    
    // 检查表结构中目标值相关字段
    db.all("PRAGMA table_info(pph_records)", (err, columns) => {
        if (err) {
            console.error('❌ 表结构查询失败:', err);
        } else {
            console.log(`\n🏗️ PPH记录表结构中的目标值字段:`);
            columns.forEach(col => {
                if (col.name.includes('target') || col.name.includes('estimated') || col.name.includes('pph') || col.name.includes('achievement')) {
                    console.log(`  ${col.name}: ${col.type} (nullable: ${col.notnull === 0 ? 'YES' : 'NO'})`);
                }
            });
        }
        db.close();
    });
});
